package Mutation;

import GA.Algorithm;
import GA.Chromosome;
import GA.Gene;
import GA.Mutation;

public class FlipMutation extends Mutation {

	public FlipMutation(double mutationProb) {
		super(mutationProb);
		
	}

	protected Chromosome mutate(Chromosome chrom) {
		// Select two random points on the chromosome
		int start = 0, end = 0;
		while(start == end){
			start = Algorithm.random.nextInt(chrom.getChromosomeLenght());
			end = Algorithm.random.nextInt(chrom.getChromosomeLenght());
		}
		if (start > end){
			int aux = end;
			end = start;
			start = aux;
		}
		
		// Flip elements between this two points
		while (start < end){
			// Swap start and end elements
			Gene aux = chrom.getGene(end);
			chrom.setGene(end, chrom.getGene(start));
			chrom.setGene(start, aux);
			// Decrements both indexs
			start++;
			end--;
		}
		return chrom;
	}

}
